package com.knife.blog.web.action;

import com.knife.blog.domain.BlogNewsDir;
import com.knife.blog.logic.BlogDirService;
import com.knife.blog.logic.impl.BlogCoreServiceImpl;
import com.knife.util.CommUtil;
import com.knife.web.Module;
import com.knife.web.Page;
import com.knife.web.WebForm;
import com.knife.web.tools.IActiveUser;
import com.knife.web.tools.IPageList;

public class BlogNewsDirAction extends BaseCrudAction {

    private BlogDirService blogDirService = BlogCoreServiceImpl.getInstance();

    public Page doList(WebForm form, Module module) {
        int currentPage = CommUtil.null2Int(form.get("page"));
        int pageSize = CommUtil.null2Int(form.get("pageSize"));
        if (currentPage < 1) {
            currentPage = 1;
        }
        if (pageSize < 1) {
            pageSize = 15;
        }
        String dirId = CommUtil.null2String(form.get("dirsn"));
        if (!"".equals(dirId)) {
            IPageList pageList = blogDirService.queryBlogNewsDoc(null, null, dirId, null, null, null, currentPage, pageSize);
            CommUtil.saveIPageList2WebForm(pageList, form);
            Module mo = new Module();
            mo.setAction("/blogNewsDoc");
            return module.findPage("list");
        } else {

            return null;
        }

    }

    public IPageList doQuery(WebForm form, int currentPage, int pageSize) {
        String parentId = CommUtil.null2String(form.get("parentsn"));
        if (!"".equals(parentId)) {

            BlogNewsDir dir = blogDirService.getBlogNewsDir(parentId);
            return blogDirService.getChildDirs(dir, currentPage, pageSize);
        } else {
            return blogDirService.queryBlogNewsDir(this.getCurrentUser(form).getUserName(), currentPage, pageSize);
        }
    }

    @Override
    public Page doAdd(WebForm form, Module module, IActiveUser user) {
        BlogNewsDir dir = (BlogNewsDir) form2Obj(form);
        dir.setInputuser(user.getUserName());
        boolean ret = blogDirService.addBlogNewsDir(dir);
        if (ret) {
            form.addResult("msg", "添加成功!");
            return super.doQuery(form, module, user);
        } else {
            return module.findPage("edit");
        }
    }

    @Override
    public Page doDel(WebForm form, Module module, IActiveUser user) {
        BlogNewsDir dir = (BlogNewsDir) form2Obj(form);
        boolean ret = blogDirService.delBlogNewsDir(dir.getCid());
        form.addResult("msg", ret ? "删除成功!" : "删除失败!");
        return super.doQuery(form, module, user);
    }

    @Override
    public Page doUpdate(WebForm form, Module module, IActiveUser user) {
        BlogNewsDir dir = (BlogNewsDir) form2Obj(form);
        dir.setInputuser(user.getUserName());
        boolean ret = blogDirService.updateBlogNewsDir(dir);
        form.addResult("msg", ret ? "修改成功!" : "修改失败!");
        return super.doQuery(form, module, user);
    }

    public Page doDelBatch(WebForm form, Module module) {
        String MulitCid = CommUtil.null2String(form.get("mulitCid"));
        String[] s = MulitCid.split(",");
        int num = blogDirService.batchDelBlogNewsDir(s);
        form.addResult("msg", "成功删除了" + num + "条信息的状态!");
        return super.doQuery(form, module, this.getCurrentUser(form));
    }

    public Object form2Obj(WebForm form) {
        String cid = CommUtil.null2String(form.get("cid"));
        BlogNewsDir obj = blogDirService.getBlogNewsDir(cid);
        if (obj == null) {
            obj = new BlogNewsDir();
        }
        form.toPo(obj);
        return obj;
    }

    public void setBlogDirService(BlogDirService blogDirService) {
        this.blogDirService = blogDirService;
    }
}
